<template>
    <div class="index-lists">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="企业名称" prop="firmName">
                    <el-input class="w-[280px]" v-model="queryParams.firmName" />
                </el-form-item>
                <!--        <el-form-item label="类型" prop="type">-->
                <!--          <el-select v-model="queryParams.type" class="w-[280px]" clearable>-->
                <!--            <el-option label="类型1" value="1" />-->
                <!--            <el-option label="类型2" value="2" />-->
                <!--          </el-select>-->
                <!--        </el-form-item>-->
                <el-form-item label="类型" prop="type">
                    <el-select v-model="queryParams.type" class="w-[280px]" clearable>
                        <el-option
                            v-for="item in options"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="入驻日期" prop="dateCheck">
                    <daterange-picker
                        v-model:startTime="queryParams.dateCheckStart"
                        v-model:endTime="queryParams.dateCheckEnd"
                    />
                </el-form-item>
                <el-form-item label="合作状态" prop="coopStatus">
                    <el-select v-model="queryParams.coopStatus" class="w-[280px]" clearable>
                        <el-option label="正常" value="1" />
                        <el-option label="禁用" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item label="收款方式" prop="paymentTermid">
                    <el-select v-model="queryParams.paymentTermid" class="w-[280px]" clearable>
                        <el-option label="收款方式1" value="1" />
                        <el-option label="收款方式2" value="2" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['supplier:add']" type="primary" @click="handleAdd()">
                    <template #icon>
                        <icon name="el-icon-Plus" />
                    </template>
                    新增
                </el-button>
            </div>
            <el-table class="mt-4" size="large" v-loading="pager.loading" :data="pager.lists" stripe>
                <!--        <el-table-column label="企业名称" prop="firmName" min-width="130" sortable/>-->
                <!--        <el-table-column label="类型" prop="type" min-width="130" sortable />-->
                <!--        <el-table-column label="入驻日期" prop="dateCheck" min-width="180" sortable/>-->
                <!--        <el-table-column label="合作状态" prop="coopStatus" min-width="130" sortable/>-->
                <!--        <el-table-column label="收款方式" prop="paymentTerm" min-width="130" sortable/>-->
                <!--        <el-table-column label="操作人" prop="userName" min-width="130" sortable/>-->
                <!--        <el-table-column label="创建时间" prop="createTime" min-width="180" sortable/>-->
                <el-table-column label="产品类型" prop="type" min-width="130" sortable />
                <el-table-column label="产品名称" prop="productName" min-width="130" sortable />
                <el-table-column label="供应商名称" prop="firmName" min-width="130" sortable />
                <el-table-column label="备注" prop="remark" min-width="130" sortable />
                <el-table-column label="操作" width="120" fixed="right">
                    <template #default="{ row }">
                        <el-button
                            v-perms="['supplier:edit']"
                            type="primary"
                            link
                            @click="handleEdit(row)"
                        >
                            编辑
                        </el-button>
                        <el-button
                            v-perms="['supplier:del']"
                            type="danger"
                            link
                            @click="handleDelete(row.id)"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="supplier">
import { supplierDelete, supplierLists } from '@/api/supplier'
import { usePaging } from '@/hooks/usePaging'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    firmName: '',
    type: '',
    dateCheckStart: '',
    dateCheckEnd: '',
    coopStatus: '',
    paymentTermid: ''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: supplierLists,
    params: queryParams
})

const options = [
    {
        value: '商标类',
        label: '商标类'
    },
    {
        value: '版权类',
        label: '版权类'
    },
    {
        value: '条形码',
        label: '条形码'
    },
    {
        value: '专利',
        label: '专利'
    },
    {
        value: '国际商标专利',
        label: '国际商标专利'
    },
    {
        value: '专利类',
        label: '专利类'
    }
]

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await supplierDelete({ id })
    feedback.msgSuccess('删除成功')
    getLists()
}

getLists()
</script>
